/**********************************************************************
 * FILE : HelloWorld.java
 * CREATE DATE : 2011-12-9
 * DESCRIPTION :
 *		
 *      
 * CHANGE HISTORY LOG
 *---------------------------------------------------------------------
 * NO.|    DATE    |     NAME     |     REASON     | DESCRIPTION
 *---------------------------------------------------------------------
 * 1  | 2011-12-9 |  Sting  |    草创版本
 *---------------------------------------------------------------------              
 **********************************************************************
 */
package com.sgm.de.benchmark.lancher.web;

/**
 * @author Sting
 *         主要作用:
 *         注意事项:
 */
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.sgm.de.benchmark.WebAgent;

public class Home extends BasePage {
    private static final long serialVersionUID = 1L;
    private List<ProcessPanel> list = new ArrayList<ProcessPanel>();
    public Home() {
        Link newpage = new Link("newpage"){
            @Override
            public void onClick() {
                setResponsePage(new NewPage());
            }
        };
        newpage.setVisible(false);
        add(newpage);

        final MarkupContainer container = new WebMarkupContainer("container");
        container.setOutputMarkupId(true);
        add(container);
        final ListView<ProcessPanel> listView = new ListView<ProcessPanel>("processDetails",
                new PropertyModel<List<ProcessPanel>>(this, "list")) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<ProcessPanel> item) {
                ProcessPanel p = item.getModelObject();
                item.add(p);
            }
        };
        listView.setOutputMarkupId(true);
        container.add(listView);


        AjaxLink ab = new AjaxLink("addProcess") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                int port = WebAgent.startProcess();
                list.add(new ProcessPanel(String.valueOf(port)));
                target.add(container);
            }
        };
        add(ab);
        add(new ControlPanel("controlPanel"));
    }

    public List<ProcessPanel> getList() {
        return list;
    }

    private final class MessageForm extends Form
    {
        public MessageForm(String id, IModel model)
        {
            super(id);
            add(new TextField("messageInput", model));
        }

        protected void onSubmit()
        {
            // nothing to do here as the model is automatically updated
        }
    }
}